/*****************************
 * Copyright (c) 2009 by Hotct Co. Ltd.  All rights reserved.
 ****************************/
package com.happy.services;

import java.util.List;



import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.core.orm.hibernate.HibernateDao;
import com.core.orm.hibernate.Page;
import com.happy.common.util.AppConstants;
import com.happy.entity.Message;
import com.happy.exception.ServicesException;

/**
 * @{#} PubMessageManager.java Create on Apr 22, 2009 5:49:09 PM
 *      <p>
 *      PubMessage 站内短消息管理
 *      </p>
 * @author <a href="mailto:maopenglin@hotmail.com">毛鹏霖</a>
 * @version v 0.1
 */
@Service
@Transactional
public class MessageService {
	private HibernateDao<Message, Long> pubMessageDao;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		pubMessageDao = new HibernateDao<Message, Long>(sessionFactory,
				Message.class);
	}

	/**
	 * @param messageId
	 * @return
	 * 
	 * <pre>
	 *   方法说明:根据短消息的编号查询短消息
	 * </pre>
	 */
	@Transactional(readOnly = true)
	public Message getPubMessage(Long messageId) throws ServicesException {
		return pubMessageDao.get(messageId);
	}

	/**
	 * @param message
	 * 
	 * <pre>
	 *   方法说明:发送短消息
	 * </pre>
	 */
	
	public void sendMessage(Message message) throws ServicesException {
		pubMessageDao.save(message);
	}
   
	/**
	 * @param messageId
	 * 
	 * <pre>
	 *   方法说明:根据短消息编号删除短消息 
	 * </pre>
	 */

	public void deleteMessage(Long messageId) throws ServicesException {
//		Message message = pubMessageDao.get(messageId);
//		message.setChStatus(AppConstants.IS_STATUS_DELETE);
		pubMessageDao.delete(messageId);
//		pubMessageDao.save(message);
	}

	/**
	 * @param message
	 * 
	 * <pre>
	 *   方法说明:删除短消息 
	 * </pre>
	 */
	public void deleteMessage(Message message) throws ServicesException {
		pubMessageDao.delete(message);
	}

	/**
	 * @param sendUserId
	 * @return
	 * 
	 * <pre>
	 *   方法说明:根据发送消息人查询短信息 未分页
	 * </pre>
	 */
	public List<Message> getMessageBySendUser(Long sendUserId)
			throws ServicesException {
		return pubMessageDao.find(
				"from Message where nuSendUserId =?  and chStatus in (1,3,4,6,7) order by date desc",
				sendUserId);
	}

	/**
	 * @param page
	 * @param sendUserId
	 * @return
	 * 
	 * <pre>
	 *   方法说明:根据发送消息人查询短信息 分页查询
	 * </pre>
	 */
	@Transactional(readOnly = true)
	public Page<Message> getMessageBySendUser(Page<Message> page,
			String sendUserId) throws ServicesException {
		
		return pubMessageDao.findPage(page,
				"from Message where nuSendUserId =? and chStatus in (1,3,4,6,7) order by date desc",
				sendUserId);
	}

	/**
	 * @param page
	 * @param receiveUserId
	 * @return
	 * 
	 * <pre>
	 *   方法说明:根据收信人查询短信息 分页查询
	 * </pre>
	 */
	@Transactional(readOnly = true)
	public Page<Message> getMessageByReceiveUser(Page<Message> page,
			String receiveUserId) throws ServicesException {
		return pubMessageDao.findPage(page,
				"from Message where nuReceiveUserId =?  and  chStatus in (1,3,4,6,7) order by date desc",
				receiveUserId);
	}

	/**
	 * @param receiveUserId
	 * @return
	 * 
	 * <pre>
	 *   方法说明:根据收信人查询短信息 为分页
	 * </pre>
	 */
	@Transactional(readOnly = true)
	public List<Message> getMessageByReceiveUser(String receiveUserId)
			throws ServicesException {
		return pubMessageDao.find(
				"from Message where nuReceiveUserId =? and chStatus in (1,3,4,6,7) order by date desc",
				receiveUserId);
	}

	/**
	 * @param receiveUserId
	 * @return
	 */
	@Transactional(readOnly = true)
	public int getMessageCountByReceiveUser(String receiveUserId)
			throws ServicesException {
		return Integer
				.parseInt(pubMessageDao
						.findUnique(
								"select count(nuReceiveUserId) from Message where nuReceiveUserId=? and chStatus in (1,3,4,6,7)",
								receiveUserId).toString());
	}

	/**
	 * @param sendUserId
	 * @return
	 */
	@Transactional(readOnly = true)
	public int getMessageCountBySendUser(String sendUserId)
			throws ServicesException {
		return Integer
				.parseInt(pubMessageDao
						.findUnique(
								"select count(nuSendUserId) from Message where nuSendUserId=? and chStatus in (1,3,4,6,7)",
								sendUserId).toString());
	}

	/**
	 * @param receiveUserId
	 * @return 查看未读的短消息
	 */
	@Transactional(readOnly = true)
	public int getMesssageNotReadByReceiveUser(String receiveUserId) {
		return Integer
				.parseInt(pubMessageDao
						.findUnique(
								"select count(*) from Message where nuReceiveUserId=? and chStatus ='7'",
								receiveUserId).toString());
	}
	@Transactional(readOnly = true)
	public boolean isMymessage(String nuReceiveUserId,long msgId)
	{
		return Integer
		.parseInt(pubMessageDao
				.findUnique(
						"select count(*) from Message where nuReceiveUserId=? and nuId=?",
						nuReceiveUserId,msgId).toString())>0;
	}
}
